package com.browsesoft.baseadapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.browsesoft.PropertiesService;
import com.browsesoft.SequenceFind;
import com.browsesoft.Tools;
import com.browsesoft.util.StreamHelper;

public class FileBlobEntity implements BlobEntity {

	private FileInputStream inStream;

	private FileOutputStream outStream;

	private String templateName;

	private File template;

	/**
	 * 无参构造，相当于新建
	 */
	public FileBlobEntity() {
		String path = this.getTemplatePath();
		try {
			this.templateName = SequenceFind.getID() + "";
			this.template = new File(path + this.templateName);
			this.outStream = new FileOutputStream(this.template);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public FileBlobEntity(String id) {
		this.templateName = id;
		try {
			String path = this.getTemplatePath();
			this.template = new File(path + this.templateName);
			this.inStream = new FileInputStream(this.template);
		} catch (FileNotFoundException e) {
			throw new BlobNotFoundException(id);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	private String getTemplatePath() {
		try {
			String path = new File(Tools.getAbsolutePath())
					+ PropertiesService.getProperty("Text", "formalpath");
			return path;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	// private String getAttackmentPath() {
	// try {
	// String path = new File(Tools.getAbsolutePath())
	// + PropertiesService.getProperty("Text", "path");
	// return path;
	// } catch (Exception e) {
	// e.printStackTrace();
	// throw new RuntimeException(e);
	// }
	// }

	public void save() throws Exception {
		this.outStream.flush();
		this.outStream.close();
	}

	public void delete() throws Exception {
		boolean del = this.template.delete();
		if (!del) {
			System.out.println("删除文档" + this.template.toString() + "失败!");
		}
	}

	public void saveOrUpdate(byte[] fileBody) {
		try {
			this.outStream = (FileOutputStream) this.getOutputStream();
			this.outStream.write(fileBody);
			this.save();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public OutputStream getOutputStream() throws Exception {
		return this.outStream;
	}

	public InputStream getInputStream() throws Exception {
		return this.inStream;
	}

	public String getContent() {
		try {
			return StreamHelper.getString(this.inStream);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public String getID() {
		return this.templateName;
	}

	public void close() {
		if (this.inStream != null) {
			try {
				this.inStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (this.outStream != null) {
			try {
				this.outStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		this.template = null;
	}

	public long getLength() {
		return this.template.length();
	}
}
